let loginFB = document.getElementById("loginFB");
let signupFB = document.getElementById("signupFB");
let login = document.getElementById("login");
let signup = document.getElementById("signup");

//网址前半部分
let baseUrl = "http://www.rushmc.top/api/";

function signupPost() {
  //将表单信息打包
  let data = new FormData();
  data.append("username", document.getElementById("registerUsername").value);
  data.append("password", document.getElementById("registerPassword").value);
  data.append("name", document.getElementById("registerName").value);
  data.append("phone", document.getElementById("registerPhone").value);

  //新建xhr对象
  let xhr = new XMLHttpRequest();

  //选择post方法，网址拼接字符串
  xhr.open("POST", baseUrl + "register", true);

  // 省略请求头
  // http.setRequestHeader("Content-type","application/json");
  //发送
  xhr.send(data);

  // 服务器响应
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      let res = JSON.parse(xhr.responseText);
      if (res.code === 200) {
        //注册成功
        alert(
          "注册成功！欢迎你," +
            document.getElementById("registerName").value +
            "!\n 点下确定后5秒钟将会跳转到登录页面"
        );
        console.log(res.msg);

        //设置定时器，5秒钟后跳转登录页面
        let i = 0;
        let timer = setInterval(function () {
          if (i == 5) {
            document.getElementById("jumpLogin").click();
            document.getElementById("username").value = registerUsername.value;

            //保证样式
            document.getElementById("username").blur(this);
            clearInterval(timer);
          }
          signupFB.innerHTML = "注册成功！  " + (5 - i) + "秒后跳转到登录页面";
          i++;
        }, 1000);
      } else if (res.code === 301) {
        //用户名已存在！
        signupFB.style.color = "#ff0000";
        signupFB.innerHTML = "用户名已存在！";

        console.log(res.msg);
      } else {
        //其他错误情况
        console.log(res.msg);
        signupFB.style.color = "#ff0000";
        signupFB.innerHTML = "发生错误:" + res.code + "  请稍后再试";
      }
    }
  };
}

//触发注册的post函数发起请求
signup.onclick = function () {
  if (!document.getElementById("registerUsername").value) {
    signupFB.innerText = "用户名不能为空！";
  } else if (!document.getElementById("registerPassword").value) {
    signupFB.innerText = "密码不能为空！";
  } else {
    let confirmmsg = "确认注册？";
    if (confirm(confirmmsg)) {
      signupPost();
    }
  }
};

//登录的post函数
function loginFun() {
  //将表单信息打包
  let data = new FormData();
  data.append("username", document.getElementById("username").value);
  data.append("password", document.getElementById("password").value);


  //新建xhr对象
  let xhr = new XMLHttpRequest();

  //选择post方法，网址拼接字符串
  xhr.open("POST", baseUrl + "login", true);

  // 省略请求头
  // http.setRequestHeader("Content-type","application/json");

  //发送
  xhr.send(data);
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      let res = JSON.parse(xhr.responseText);
      if (res.code === 200) {
        alert("登录成功！即将跳转。。。");
        console.log(res.msg);
        window.location.href = "index.html";
      } else if (res.code === 300) {
        console.log(res.msg);
        loginFB.innerText = "账号或密码错误!";
      } else {
        console.log(res.msg);
        loginFB.innerHTML = "发生错误:" + res.code + "  请稍后再试";
      }
    }
  };
}


//登录触发登录的post函数
login.onclick = function () {
  if (!document.getElementById("username").value) {
    loginFB.innerText = "请输入用户名！";
  } else if (!document.getElementById("password").value) {
    loginFB.innerText = "请输入密码！";
  } else {
    loginFun();
  }
};
